﻿/*
ParallelPlatform Project - A web platform to distribute and share knowledge
Online Editor - An online tool to create and play animations
Copyright (C) 2011 Daniel Guerrero Martínez & Sergio Rodríguez Lumley

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package Classes{
	
	import flash.display.MovieClip;
	import flash.utils.Timer;
	import flash.events.TimerEvent;
	import fl.transitions.Tween;
	import fl.transitions.easing.*;
	import fl.transitions.TweenEvent;
	
	public class LoadedNotificator extends MovieClip{
		private var timer:Timer;
		private var tween:Tween;
		public function LoadedNotificator(){
			this.stop();
		}
		
		public function begin():void{
			this.gotoAndStop(1);
			if(timer != null){
				timer.stop();
				timer.removeEventListener(TimerEvent.TIMER_COMPLETE, disappear);
			}
			
			if(tween != null){
				tween.stop();
				tween.removeEventListener(TweenEvent.MOTION_FINISH, removeThis);
			}
		}
		
		public function finish(suc:Boolean = true):void{
			if(suc) this.gotoAndStop(2);
			else this.gotoAndStop(3);
			
			timer = new Timer(5000, 1);
			timer.stop();
			timer.addEventListener(TimerEvent.TIMER_COMPLETE, disappear);
			timer.start();
		}
		
		
		private function disappear(evt:TimerEvent):void{
			timer.stop();
			timer.removeEventListener(TimerEvent.TIMER_COMPLETE, disappear);
			
			tween = new Tween(this, 'alpha', Regular.easeInOut, 1.0, 0.0, 2, true);
			tween.addEventListener(TweenEvent.MOTION_FINISH, removeThis);
		}
		
		private function removeThis(evt:TweenEvent):void{
			tween.stop();
			tween.removeEventListener(TweenEvent.MOTION_FINISH, removeThis);
			parent.removeChild(this);
		}
		
	}
	
}